﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using BugTracker.BTGridView;
using BugTracker.BusinessEntities.Entities.Company;
using BugTracker.BusinessEntities.Entities.Project;
using BugTracker.BusinessLogic.BTUser;
using BugTracker.BusinessLogic.Company;
using BugTracker.BusinessLogic.Project;
using BugTracker.Core.Infrastructure.Exceptions;
using BugTracker.WebUX.WebCode;
using Microsoft.Practices.Unity;
using BugTracker.BusinessLogic.Project.ProjectDetail;

namespace BugTracker.WebUX.Workspace.Modules
{
    public partial class UCProjectList : System.Web.UI.UserControl
    {

        #region load

        protected void Page_Load(object sender, EventArgs e)
        {
            ModelBTGridView.BTGridViewPageIndexChanging += OnPageIndexChanged;
            if (!Page.IsPostBack)
            {
                BindData(0, ModelBTGridView.PageSize);
            }
        }

        #endregion

        #region infobox

        private void SetInfoBoxError(string message)
        {
            ActionInfoBox.Visible = true;
            ActionInfoBox.Text = message;
            ActionInfoBox.MessageType = InfoBox.InfoBox.MsgType.red;
        }

        #endregion

        #region methods

        private void OnPageIndexChanged(object sender, BTPagerEventArgs e)
        {
            BindData(e.PageIndex, ModelBTGridView.PageSize);
        }

        private void BindData(int pageIndex, int pageCount)
        {
            int count = 0;
            if (ModelBTGridView.PTSorter == null)
            {
                ModelBTGridView.PTSorter = new Sorter("Name", SortDirection.Ascending);
            }
            var data = Global.IoC.Container.Resolve<IProjectDetailService>()
                .PageSort(
                            pageIndex,
                            pageCount,
                            ModelBTGridView.PTSorter.Column,
                            ModelBTGridView.PTSorter.GetDirection(),
                            out count
                            );
            ModelBTGridView.VirtualCount = count;
            ModelBTGridView.DataSource = data;
            ModelBTGridView.DataBind();
        }

        protected void ModelBTGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton deleteImgBtn = e.Row.FindControl("DeleteImageButton") as ImageButton;
                if (deleteImgBtn != null)
                {
                    deleteImgBtn.OnClientClick = Resources.Phrases.JavaScriptDeleteConfirm;
                }

                Image img = e.Row.FindControl("PlatformImage") as Image;
                if (img != null)
                {
                    img.ImageUrl = BTHttpApplication.AppPaths.FetchImageHandler + "?p1=7&p2=" +
                        ((Project)e.Row.DataItem).PlatformTypeID.ToString();
                }
            }
        }

        protected void ModelBTGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Select"))
            {
                GridViewRow row = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer;
                string redirectUrl = BTHttpApplication.AppPaths.ProjectDetail +
                    "?" +
                    BTHttpApplication.UrlParam.PROJECT_ID +
                    "=" +
                    this.ModelBTGridView.DataKeys[row.RowIndex].Value.ToString();
                Response.Redirect(redirectUrl);
            }
            else if (e.CommandName.Equals("Delete"))
            {
                GridViewRow row = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer;
                try
                {
                    int id = Convert.ToInt32(this.ModelBTGridView.DataKeys[row.RowIndex].Value);
                    Global.IoC.Container.Resolve<IProjectService>().DeleteObject(id);
                    ActionInfoBox.Text = Resources.Phrases.DeleteSuccessful;
                    ActionInfoBox.MessageType = InfoBox.InfoBox.MsgType.green;
                    BindData(0, this.ModelBTGridView.PageSize);
                }
                catch (BugTrackerExpection exc)
                {
                    SetInfoBoxError(exc.Message);
                }
            }
        }

        protected void ModelBTGridView_RowDeleting(object sender, GridViewDeleteEventArgs e) { }

        protected void AddImageButton_Click(object sender, EventArgs e)
        {
            string redirectUrl = BTHttpApplication.AppPaths.ProjectDetail;
            Response.Redirect(redirectUrl);
        }

        #endregion
    }
}